package com.start_eternity.oasissafewalk.service.impl;

import com.start_eternity.oasissafewalk.mapper.OrderDatesMapper;
import com.start_eternity.oasissafewalk.pojo.Logg;
import com.start_eternity.oasissafewalk.pojo.OrderDates;
import com.start_eternity.oasissafewalk.service.LoggService;
import com.start_eternity.oasissafewalk.service.OrderDatesService;
import com.start_eternity.oasissafewalk.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * author:ZhangT
 * Date:2024 / 03 / 29 / 23:40
 * Description:
 */
@Service
public class OrderDatesServiceImpl implements OrderDatesService {

    @Autowired
    private OrderDatesMapper orderDatesMapper;

    @Autowired
    private LoggService loggService;

    @Autowired
    private HttpServletRequest request;

    //根据Id查询
    @Override
    public OrderDates query(Integer orderDateId) {
        OrderDates query = orderDatesMapper.query(orderDateId);

        Logg logg = new Logg();
        logg.setLoggTime(LocalDateTime.now());
        String jwt = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(jwt);
        String name = (String)claims.get("username");
        logg.setLoggName(name);
        logg.setLoggDescription("执行了查询id:" + orderDateId);
        loggService.insert(logg);

        return query;
    }

    //订单天数的添加
    @Override
    public int saveOrderDates(OrderDates orderDates) {
        int i = orderDatesMapper.insertOrderDates(orderDates);

        Logg logg = new Logg();
        logg.setLoggTime(LocalDateTime.now());
        String jwt = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(jwt);
        String name = (String)claims.get("username");
        logg.setLoggName(name);
        logg.setLoggDescription("保存订单天数的内容为:" + orderDates);
        loggService.insert(logg);

        return i;
    }

    //修改订单的天数
    @Override
    public int changeNumDays(OrderDates orderDates) {
        int i = orderDatesMapper.updateNumDays(orderDates);

        Logg logg = new Logg();
        logg.setLoggTime(LocalDateTime.now());
        String jwt = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(jwt);
        String name = (String)claims.get("username");
        logg.setLoggName(name);
        logg.setLoggDescription("修改订单天数的内容为:" + orderDates);
        loggService.insert(logg);

        return i;
    }
}
